package com.hzhc.utils;

import com.hzhc.enumBean.ResultUtilCodeEnum;
import lombok.Data;

import java.util.Map;

@Data
public class ResultUtil<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    private long count = 0;

    /**
     * 合计
     */
    private Map<String, Object> totalRow;

    //返回数据
    private T data;

    /**
     * 总页数
     */
    private long totalPage = (long) 0;

    public ResultUtil(){}

    // 返回数据
    protected static <T> ResultUtil<T> build(T data) {
        ResultUtil<T> result = new ResultUtil<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> ResultUtil<T> build(ResultUtilCodeEnum ResultUtilCodeEnum) {
        ResultUtil<T> result = new ResultUtil<T>();
        result.setCode(ResultUtilCodeEnum.getCode());
        result.setMessage(ResultUtilCodeEnum.getMessage());
        return result;
    }


    public static <T> ResultUtil<T> build(T body, Integer code, String message) {
        ResultUtil<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> ResultUtil<T> build(Integer code, String message) {
        ResultUtil<T> result = new ResultUtil<T>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> ResultUtil<T> build(T body, ResultUtilCodeEnum ResultUtilCodeEnum) {
        ResultUtil<T> result = build(body);
        result.setCode(ResultUtilCodeEnum.getCode());
        result.setMessage(ResultUtilCodeEnum.getMessage());
        return result;
    }

    public static <T> ResultUtil<T> build(T body,Integer count, ResultUtilCodeEnum ResultUtilCodeEnum) {
        ResultUtil<T> result = build(body);
        result.setCode(ResultUtilCodeEnum.getCode());
        result.setCount(count);
        result.setMessage(ResultUtilCodeEnum.getMessage());
        return result;
    }


    // 返回数据，数据总数，分页总数和提示消息
    public static <T> ResultUtil<T> build(T body,long count,long totalPage ,ResultUtilCodeEnum ResultUtilCodeEnum) {
        ResultUtil<T> result = build(body);
        result.setCode(ResultUtilCodeEnum.getCode());
        result.setCount(count);
        result.setTotalPage(totalPage);
        result.setMessage(ResultUtilCodeEnum.getMessage());
        return result;
    }

    // 返回数据，数据总数，分页总数，合计数据和提示消息
    public static <T> ResultUtil<T> build(T body, long count, long totalPage , Map<String,Object> totalRow, ResultUtilCodeEnum ResultUtilCodeEnum) {
        ResultUtil<T> result = build(body);
        result.setCode(ResultUtilCodeEnum.getCode());
        result.setCount(count);
        result.setTotalPage(totalPage);
        result.setTotalRow(totalRow);
        result.setMessage(ResultUtilCodeEnum.getMessage());
        return result;
    }

    public static<T> ResultUtil<T> ok(){
        return ResultUtil.ok(null);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> ResultUtil<T> ok(T data){
        ResultUtil<T> result = build(data);
        return build(data, ResultUtilCodeEnum.SUCCESS);
    }

    public static<T> ResultUtil<T> fail(){
        return ResultUtil.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> ResultUtil<T> fail(T data){
        ResultUtil<T> result = build(data);
        return build(data, ResultUtilCodeEnum.FAIL);
    }

    public ResultUtil<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public ResultUtil<T> code(Integer code){
        this.setCode(code);
        return this;
    }


}


